import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_oupay/flutter_oupay.dart';
import 'package:flutter_oupay/oupay_options.dart';
import 'package:flutter_oupay/oupay_result.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  static final Dio dio = Dio(BaseOptions(
    baseUrl: "https://www.wanandroid.com",
    connectTimeout: 5000,
    receiveTimeout: 3000,
  ));
  @override
  void initState() {
    super.initState();
    initPlatformState();

    ///网络请求配置

    OupayOptions opt = new OupayOptions(isSandboxByUn:true,isSandboxByAli:true,wechatId:'wx06231605d005bcbe',isSandboxByCmb:true,cmbAppId:'0010000516',isSandboxByCcb: false);
    FlutterOupay.setOupayOptions(opt);
   //  OupayOptions opt = new OupayOptions(isSandboxByUn:true,isSandboxByAli:true,wechatId:'wx06231605d005bcbe',isSandboxByCmb:true,cmbAppId:'0010000516');
   //  FlutterOupay.setOupayOptions(opt);
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await FlutterOupay.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          //child: Text('Running on: $_platformVersion\n'),
          child:Column(
            children:[
              RaisedButton(
                  child: Text('银联支付'),
                  onPressed: () async {

                    String url = "http://123.207.54.252:8080/demo/api_05_app/Form_6_2_AppConsume.aspx";//银联地址
                    FormData formData = FormData.fromMap({
                      "orderId": "1111111${DateTime.now().day}${DateTime.now().second}",
                      "txnAmt": 100,//金额是按分走的
                    });
                    Response response = await dio.post(url, data: formData);
                    Map<String, String> payInfo = new Map<String, String>();
                    payInfo['channelId'] = '15';
                    payInfo['channelData'] = response.toString();
                    OupayResult result = await FlutterOupay.ouPay(payInfo);
                    print(result.toString());
                    // setState(() {
                    //   _restText = result.toString();
                    // });
                  }),
              RaisedButton(
                  child: Text('建行支付'),
                  onPressed: ()  async {
                    Map<String, String> payInfo = new Map<String, String>();
                    payInfo['channelId'] = '15';
                    payInfo['channelData'] = "1234566677";
                    OupayResult result = await FlutterOupay.ouPay(payInfo);
                    print(result.toString());
                    // setState(() {
                    //   _restText = result.toString();
                    // });
                  }),
            ],
          ),

        ),
      ),
    );
  }

}
